package com.kstech.mp3604.fragments

import com.blankj.utilcode.util.SPUtils
import com.kstech.mp3604.base.BaseDialogFragment
import com.kstech.mp3604.base.Global
import com.kstech.mp3604.databinding.FragmentDialogConfirmBinding
import com.kstech.mp3604.manager.serialComm.SerialManager
import com.kstech.mp3604.manager.serialComm.SerialUiListener

/**
 * 确认是否发送标定信息
 */
class DialogConfirmFragment :
    BaseDialogFragment<FragmentDialogConfirmBinding>(FragmentDialogConfirmBinding::inflate) {

    private var address = ""
    private var text = ""
    private lateinit var listener: SerialUiListener
    private var value = 0

    override fun onInitView() {

        binding.apply {
            textView2.text = "是否发送:$text"
            cancelButton.setOnClickListener {
                dialog!!.cancel()
            }
            confirmButton.setOnClickListener {
                dialog!!.cancel()
                if (address == "2009") {
                    SerialManager.sendFactoryModel(value, listener)
                } else if (address == "2105") {
                    //存储到数据库
                    SPUtils.getInstance().put(Global.WARE_HOUSE_SETTING_ID, value)
                    listener.saveWareHouseSettingId()
                } else {
                    SerialManager.sendBiaoDingById(address, listener)
                }
            }
        }
    }

    fun bindData(
        listener: SerialUiListener, text: String, address: String, value: Int
    ): DialogConfirmFragment {
        this.listener = listener
        this.address = address
        this.text = text
        this.value = value
        return this
    }
}